package com.larissa.quiz;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.SavedStateHandle;
import androidx.lifecycle.ViewModel;

import java.util.ArrayList;
import java.util.List;

public class QuizViewModel extends ViewModel {
    private static final String TAG="QuizViewModel";

    public LiveData<List<Question>> questionsLiveData;
    public List<Question>questions=new ArrayList<>();
    public int currentIdx=0;
    public int correctCount=0;
    private QuizRepository repository;

    public String getQuestionText() {
        if(questions.size()==0){
            return "";
        }
        if(currentIdx==-1)
            return questions.get(questions.size()-1).getQuestionText();
        else
            return questions.get(currentIdx).getQuestionText();
    }

    public boolean isAnswer() {
        if(currentIdx==-1)
            return questions.get(questions.size()-1).isQuestionAnswer();
        else
            return questions.get(currentIdx).isQuestionAnswer();
    }

    public QuizViewModel(){
        repository=QuizRepository.getInstance();
        questionsLiveData=repository.getQuestions();
    }

    public void moveToNext(){
        currentIdx++;
        if(currentIdx>=questions.size()){
            currentIdx=-1;
        }
    }

    @Override
    protected void onCleared(){
        super.onCleared();
        Log.d(TAG,"QuizViewModel instance about to be destroyed");
    }
}
